import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:fluttertoast/fluttertoast.dart';
import 'package:my_flutter_tool/widgets/app_bottom_sheet.dart';
import 'package:my_flutter_tool/widgets/dialog/app_dialog.dart';
import 'package:my_flutter_tool/widgets/dialog/dialog_bg.dart';
import 'package:my_flutter_tool/widgets/dialog/tips.dart';

export 'package:flutter_screenutil/size_extension.dart';

///应用通用工具类
///复制到项目中更改名称
class A {
  ///滚动物理封装
  static const appPhysics =
      const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics());

  ///显示Toast
  static Future<void> t(String msg,
      {ToastGravity gravity = ToastGravity.CENTER}) async {
    await Fluttertoast.cancel();
    await Fluttertoast.showToast(
      msg: msg ?? '',
      gravity: gravity,
    );
  }

  ///跳转新路由
  ///* `isRemove` 是否销毁旧路由，默认`false`
  static Future<T> jump<T>(BuildContext context, Widget page,
      {bool isRemove = false}) async {
    if (isRemove)
      return await Navigator.of(context).pushAndRemoveUntil(
          CupertinoPageRoute(builder: (_) => page), (_) => false);

    return await Navigator.of(context)
        .push(CupertinoPageRoute(builder: (_) => page));
  }

  ///显示底部弹框
  static Future<T> showBottom<T>(
      BuildContext context, AppBottomSheetBuilder builder) async {
    return await showModalBottomSheet<T>(
      context: context,
      elevation: 0,
      barrierColor: Colors.black12,
      backgroundColor: Colors.red,
      builder: (_) {
        return AppBottomSheet(builder: builder);
      },
    );
  }

  ///完全自定义对话框
  static Future<T> customDialog<T>(
    BuildContext context,
    Widget child, {
    Widget Function(BuildContext) builder,
    bool barrierDismissible = true,
    Color barrierColor,
    bool useSafeArea = true,
    bool useRootNavigator = true,
    RouteSettings routeSettings,
    bool bottom = false,
  }) async {
    return await showDialog<T>(
      context: context,
      child: DialogBg(child: child, bottom: bottom),
      builder: builder,
      barrierDismissible: barrierDismissible,
      barrierColor: barrierColor,
      useSafeArea: useSafeArea,
      useRootNavigator: useRootNavigator,
      routeSettings: routeSettings,
    );
  }

  ///定制当前应用的对话框
  static Future<T> dialog<T>(
    BuildContext context,
    Widget child, {
    EdgeInsetsGeometry padding,
    EdgeInsetsGeometry margin,
    bool bottom = false,
  }) async {
    return await customDialog<T>(
      context,
      AppDialog(
        child: child,
        padding: padding,
        margin: margin,
        bottom: bottom,
      ),
      bottom: bottom,
    );
  }

  ///显示提示
  static Future<T> tips<T>(
    BuildContext context, {
    String title,
    String content,
    bool bottom = false,
    String checkText = "确认",
    String cancelText = "取消",
    Function onCancel,
    Function onCheck,
  }) async {
    return await dialog<T>(
      context,
      Tips(
        title: title,
        content: content,
        bottom: bottom,
        checkText: checkText,
        cancelText: cancelText,
        onCancel: onCancel,
        onCheck: onCheck,
      ),
      padding:
          EdgeInsets.only(top: bottom ? 40.w : 60.w, bottom: bottom ? 60.w : 0),
      bottom: bottom,
    );
  }

  ///销毁路由或退出APP
  ///* `value` 返回值，`exit` 是否关闭应用
  static void back<T extends Object>(BuildContext context,
      {dynamic value, bool exit = false}) {
    if (exit) {
      SystemNavigator.pop();
    } else
      Navigator.pop(context, value);
  }

  ///初始化屏幕
  ///* `standardWidth` 设计稿屏幕宽度，默认`1080`
  static void initScreen(BuildContext context, {double standardWidth = 1080}) {
    ScreenUtil.init(context);
  }

  ///隐藏状态栏
  static void hideStatusBar({bool hideNavBar = false}) {
    SystemChrome.setEnabledSystemUIOverlays(
        hideNavBar ? [] : [SystemUiOverlay.bottom]);
  }

  ///显示状态栏
  static void showStatusBar({bool dark = true}) {
    SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    SystemChrome.setSystemUIOverlayStyle(
        dark ? SystemUiOverlayStyle.dark : SystemUiOverlayStyle.light);
  }

  ///取消焦点
  static void unFocus(BuildContext context) {
    FocusScope.of(context).unfocus();
  }
}
